



class GetOpenid {
  static get_openid() {
    wx.checkSession({
      success: function (res) {
        
        
      },
      fail: function (res) {
        wx.login({
          success: res => {
            if (res.code) {
              wx.request({
                url: 'https://qrcode.xiaosikeji.com/api/user/openid',
                data: {
                  'js_code': res.code,
                },
                header: {
                  "Content-Type": "application/x-www-form-urlencoded"
                },
                method: 'POST',
                success: function (res) {
                  console.log('1')
                  wx.setStorageSync('openId', res.data.data.open_id)
                  wx.request({
                    url: 'https://qrcode.xiaosikeji.com/api/user/userinfo',
                    data: {
                      'open_id': res.data.data.open_id
                    },
                    header: {
                      "Content-Type": "application/x-www-form-urlencoded"
                    },
                    method: 'POST',
                    success: function (res) {
                      console.log(2)
                      console.log(res)
                      if(res.data.data !='unbind') {
                        wx.setStorageSync('userId', res.data.data.userid)
                        wx.setStorageSync('cop_name', res.data.data.cop_name)
                        wx.setStorageSync('name', res.data.data.name)
                        wx.setStorageSync('cop_id', res.data.data.cop_id)
                      }
                      else{
                        console.log(3)
                        console.log(res)
                        wx.showModal({
                          title: '提示',
                          content: '您还没有绑定',
                          success: function (res) {
                            if (res.confirm) {
                              console.log('用户点击确定')
                              wx.navigateTo(
                                {
                                  url: '../../pages/selectpage/selectpage'
                                }
                              )
                            }
                          }
                        })
                      }
                    },
                  })
                }
              })
            }
          }
        })
      }
    })
  }

  static getopenid() {
    wx.checkSession({
      success: function (res) {

      },
      fail: function (res) {
        wx.login({
          success: res => {
            if (res.code) {
              wx.request({
                url: 'https://qrcode.xiaosikeji.com/api/user/openid',
                data: {
                  'js_code': res.code,
                },
                header: {
                  "Content-Type": "application/x-www-form-urlencoded"
                },
                method: 'POST',
                success: function (res) {
                  console.log('1' + res)
                  wx.setStorageSync('openId', res.data.data.open_id)
                }
              })
            }
          }
        })
      }
    })
  }
  // 获取用户信息  将用户信息缓存下来
  static get_userinfo_to_main(){
    console.log('获取用户信息')
    wx.request({
      url: 'https://qrcode.xiaosikeji.com/api/user/userinfo',
      data: {
        'open_id': wx.getStorageSync('openId')
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: 'POST',
      success: function (res) {
        if (res.data.data =='unbind'){
          wx.showModal({
            title: '提示',
            content: '您还没有绑定',
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
                wx.navigateTo(
                  {
                    url: '../../pages/selectpage/selectpage'
                  }
                )
              }
            }
          })
        }
        else{
          wx.setStorageSync('userId', res.data.data.userid)
          wx.setStorageSync('cop_name', res.data.data.cop_name)
          wx.setStorageSync('name', res.data.data.name)
          wx.setStorageSync('cop_id', res.data.data.cop_id)
        }
      },
    })

  } 
    
  
// 获取用户信息 如果获取不到用户信息 跳转绑定页面

  static get_userinfo() {
    console.log(wx.getStorageSync('openId'))
    wx.request({
      url: 'https://qrcode.xiaosikeji.com/api/user/userinfo',
      data: {
        'open_id': wx.getStorageSync('openId')
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: 'POST',
      success: function (res) {
        if (res.data.data) {
          wx.setStorageSync('userId', res.data.data.userid)
          wx.setStorageSync('cop_name', res.data.data.cop_name)
          wx.setStorageSync('name', res.data.data.name)
          wx.setStorageSync('cop_id', res.data.data.cop_id)
        }
        else {
          wx.navigateTo(
            {
              url: '../../pages/selectpage/selectpage'
            }
          )
        }
      },
    })
  }

  static wxPromisify(fn) {
    return function (obj = {}) {
      return new Promise((resolve, reject) => {
        obj.success = function (res) {
          resolve(res)
        }
        obj.fail = function (res) {
          reject(res)
        }
        fn(obj)
      })
    }
  }
}


module.exports = GetOpenid


// function get_openid() {
//   wx.login({
//     success: function (res) {
//       wx.request({
//         url: 'https://qrcode.xiaosikeji.com/api/user/openid',
//         data: {
//           'js_code': res.code,
//         },
//         header: {
//           "Content-Type": "application/x-www-form-urlencoded"
//         },
//         method: 'POST',
//         success: function (res) {
//           console.log(res.data.data.open_id)
//           if (res.data.data.open_id) {
//             wx.setStorageSync('openId', res.data.data.open_id);
//           }
//           else {
//             wx.showModal({
//               title: '网络或服务器故障',
//             })
//           }
//         },
//         fail: function () {
//           wx.showModal({
//             title: '警告通知',
//             content: '网络或服务器故障'
//           })
//         }
//       })
//     },
//     fail: function () {
//       wx.showModal({
//         title: '警告通知',
//         content: '您点击了拒绝授权,将无法正常显示二维码信息,点击确定重新获取授权。',
//         success: function (res) {
//           if (res.confirm) {

//           }
//         }
//       })
//     }
//   })
// }
















